﻿Imports System.Data.SqlClient
Public Class ItensCompraDAL
#Region "Incluir"
    Public Function inserir(com_codigo As Integer, itc_custoreal As Decimal, itc_frete As Decimal, itc_icms As Decimal, itc_ipi As Decimal, itc_margcontrib As Decimal, itc_porcentagem As Decimal, itc_qtde As Integer, itc_substtrib As Decimal, itc_valortotal As Decimal, itc_valorunitario As Decimal, pro_codigo As Integer, itc_desconto As Decimal, itc_precovenda As Decimal) As Integer
        Dim cmd = New SqlCommand
        Dim codigo As Integer
        Try
            cmd.CommandText = "insert into Estoque.Gen_ItensCompra (com_codigo,itc_custoreal,itc_frete,itc_icms,itc_ipi,itc_margcontrib,itc_porcentagem,itc_qtde,itc_substtrib,itc_valortotal,itc_valorunitario,itc_desconto,itc_precovenda, pro_codigo) " & _
            "values (@com_codigo,@itc_custoreal,@itc_frete,@itc_icms,@itc_ipi,@itc_margcontrib,@itc_porcentagem,@itc_qtde,@itc_substtrib,@itc_valortotal,@itc_valorunitario,@itc_desconto,@itc_precovenda,@pro_codigo)"
            cmd.Parameters.Add(New SqlParameter("@com_codigo", SqlDbType.Int)).Value = com_codigo
            cmd.Parameters.Add(New SqlParameter("@itc_custoreal", SqlDbType.Decimal)).Value = itc_custoreal
            cmd.Parameters.Add(New SqlParameter("@itc_frete", SqlDbType.Decimal)).Value = itc_frete
            cmd.Parameters.Add(New SqlParameter("@itc_icms", SqlDbType.Decimal)).Value = itc_icms
            cmd.Parameters.Add(New SqlParameter("@itc_ipi", SqlDbType.Decimal)).Value = itc_ipi
            cmd.Parameters.Add(New SqlParameter("@itc_margcontrib", SqlDbType.Decimal)).Value = itc_margcontrib
            cmd.Parameters.Add(New SqlParameter("@itc_porcentagem", SqlDbType.Decimal)).Value = itc_porcentagem
            cmd.Parameters.Add(New SqlParameter("@itc_qtde", SqlDbType.Int)).Value = itc_qtde
            cmd.Parameters.Add(New SqlParameter("@itc_substtrib", SqlDbType.Decimal)).Value = itc_substtrib
            cmd.Parameters.Add(New SqlParameter("@itc_valortotal", SqlDbType.Decimal)).Value = itc_valortotal
            cmd.Parameters.Add(New SqlParameter("@itc_valorunitario", SqlDbType.Decimal)).Value = itc_valorunitario
            cmd.Parameters.Add(New SqlParameter("@itc_desconto", SqlDbType.Decimal)).Value = itc_desconto
            cmd.Parameters.Add(New SqlParameter("@itc_precovenda", SqlDbType.Decimal)).Value = itc_precovenda
            cmd.Parameters.Add(New SqlParameter("@pro_codigo", SqlDbType.Int)).Value = pro_codigo
            codigo = objconexao.executaCmd(cmd)
        Catch ex As Exception
            Throw ex
        Finally
            cmd = Nothing
        End Try
        Return codigo
    End Function
#End Region

#Region "Excluir"
    Public Function excluirtodositens(com_codigo As Integer) As Integer
        Dim cmd = New SqlCommand
        Dim codigo As Integer
        Try
            cmd.CommandText = "delete from Estoque.Gen_ItensCompra where com_codigo=@com_codigo"
            cmd.Parameters.Add(New SqlParameter("@com_codigo", SqlDbType.Int, 1)).Value = com_codigo
            codigo = objconexao.executaCmd(cmd)

        Catch ex As Exception
            Throw ex

        Finally
            cmd = Nothing

        End Try
        Return codigo
    End Function
#End Region

#Region "Buscar"
    Function buscar(ByVal descricao As String, tipo As Integer) As DataTable
        Dim cmd As SqlCommand
        Dim tab As DataTable
        Try
            cmd = New SqlCommand
            Select Case tipo
                Case 1
                    'por codigo
                    cmd.CommandText = "select pro_codigo,pec_codbarra, pro_descricao,itc_qtde, itc_valorunitario, itc_ipi, itc_substtrib, itc_margcontrib, itc_porcentagem, itc_custoreal, itc_valortotal, itc_frete, itc_precovenda, itc_desconto from vw_EstItensCompra where  com_codigo= @codigo"
                    cmd.Parameters.Add(New SqlParameter("@codigo", SqlDbType.Int)).Value = Val(descricao)

                Case Else
                    'todos
                    cmd.CommandText = "select * from Estoque.Gen_ItensCompra"
                    cmd.Parameters.Add(New SqlParameter("@codigo", SqlDbType.Int)).Value = Val(descricao)

            End Select
            tab = objconexao.executaConsulta(cmd)
            tab.DefaultView.AllowNew = False
        Catch ex As Exception
            Throw ex
        Finally
            cmd = Nothing
        End Try
        Return tab
    End Function
#End Region
End Class
